<?php
/**
* Mô tả:
*
* Lớp quản lý Portal
*
* @access 	public
* @version	1.0
* @since  	June-2008
* @deprecated	CPortal base class
* @package 	packages/system
* @subpackage 	lib
*/
class CPortal extends CRecord
{
	static $current = false;
	static $default = 'vict';
	static $components = array();
	static $root_url;
	static $static_url;
	static $currency;
	static $script;
	static $estore_id;
	static $settings;
	function __construct($data)
	{
		parent::__construct($data);
		if($this->setting('default-portal'))
		{
			CPortal::$default = $this->setting('default-portal');
		}
		CPortal::$root_url = 'http:/'.'/'.(isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'').'/';
		CPortal::$static_url = $this->setting('static-url')?$this->setting('static-url'):CPortal::$root_url;
		CPortal::$currency = CURRENCY;		
	}
	/**/
	static function settings($name = '')
	{
		if(isset(self::$settings)){
			$settings = self::$settings;
		}else{
			$sets = CTable('config')->select_all();
			$settings = array();
			foreach ($sets as $set) {
				$settings[$set['name']] = $set['value'];
			}
			self::$settings = $settings;
		}
		
		if($name == '') 
			return $settings;
		else
			return isset($settings[$name])?$settings[$name]:'';
	}
	static function get_portals()
	{
		return CList(CList(CPortal::$components)->filter(array('type'=>'PORTAL')))->order('name')->items;
	}
	static function get_path($id)
	{
		if(isset(CPortal::$components['PORTAL.'.$id]))
		{
			return CPortal::$components['PORTAL.'.$id]['path'];
		}
	}
	static function get_xml_data($portal)
	{
		if(!DEBUG and $data = CSystem::$cachelayout->get('cache_portal_'.$portal['name']))
		{
		}
		else
		if(isset(CPortal::$components['PORTAL.'.$portal['name']]))
		{	
			//$data = CXml(CPortal::$components['PORTAL.'.$portal['name']]['path'].'/portal.xml')->decode();
			//Khoanb Edit 29.08.09
			$data = CXml(ROOT_DIR.CPortal::$components['PORTAL.'.$portal['name']]['path'].'/portal.xml')->decode();
			$data['path'] = $portal['path'];
			$data['name'] = $portal['name'];
			$data['parent'] = $portal['parent'];
			$parent = trim($portal['parent']);
			///echo 'PORTAL.'.$parent;
			if(isset(CPortal::$components['PORTAL.'.$parent]))
			{
				$default_portal = CPortal::$components['PORTAL.'.$parent];
				$default_data = CPortal::get_xml_data($default_portal);
				$data = array_merge($default_data,$data);
				//debug($data);
				$data['settings'] = array_merge($default_data['settings'],$data['settings']);
			}
			CSystem::$cachelayout->set('cache_portal_'.$portal['name'], $data);
		}
		else
		{
			return array();
		}
		return $data;
	}

	static function get_child_portals($id){
		return CList(CList(CPortal::$components)->filter(array('type'=>'PORTAL','parent'=>$id)))->order('name')->items;
	}

	static function get_portal_path($id = '')
	{
		if(!$id)
		{
			$id = portal()->id;
		}
		if(isset(CPortal::$components['PORTAL.'.$id]) and CPortal::$components['PORTAL.'.$id]['parent'])
		{
			return '.'.$id.CPortal::get_portal_path(CPortal::$components['PORTAL.'.$id]['parent']);
		}
		else
		{
			return '.'.$id.'.';
		}
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	static function get_data($id) {
	  if ($ret = @CPortal::$components['PORTAL.'.$id]) return $ret;
		return array();
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $portal
	 * @return unknown
	 */
	function skin($portal = false)
	{
		if(!$portal) {
			return $this->path.'/skins/default';
		} else {
			if($portal===true) {
				$portal = CPortal::$default;
			}
			$skins 	= 'default';
			if(CPortal::$current->data['settings']['skin']){
				$skins = CPortal::$current->data['settings']['skin'];
			}

			// Khoanb Edit 17.04.2009
			// return CPortal::get_path($portal).'/skins/default';
			return $this->path.'/skins/'.$skins;
		}
	}
}

/**
 * Create new portal
 *
 * @param array $data
 * @return CPortal
 */
function CPortal($data)
{
	return new CPortal($data);
}

/**
 * Get current portal
 *
 * @return CPortal
 */
function &portal()
{
	return CPortal::$current;
}
?>